﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TestXNA3.Components;
using System.IO;
using TestXNA3.Resources;

namespace TestEditor
{
    public partial class StaticMeshComponentProperties : Form
    {
        public StaticMeshComponent StaticMeshComponent = null;
        public StaticMeshComponentProperties()
        {
            InitializeComponent();
            openSM.InitialDirectory = Directory.GetCurrentDirectory() + "\\Content\\StaticMeshes\\";
        }

        public void Init()
        {
            staticMeshName.Text = StaticMeshComponent.MeshName;
            if (StaticMeshComponent.StaticMesh != null && StaticMeshComponent.StaticMesh.Shader != null)
                shaderProps.SelectedObject = StaticMeshComponent.StaticMesh.Shader;
        }

        private void browseSM_Click(object sender, EventArgs e)
        {
            openSM.ShowDialog();
            if (openSM.FileName.Length > 0)
            {
                string fullPath = openSM.FileName;
                string finalStaticMeshName = fullPath.Substring(openSM.InitialDirectory.Length);
                finalStaticMeshName = finalStaticMeshName.Substring(0, finalStaticMeshName.Length - 4);
                StaticMeshComponent.MeshName = finalStaticMeshName;
                staticMeshName.Text = finalStaticMeshName;
                shaderProps.SelectedObject = StaticMeshComponent.StaticMesh.Shader;
            }
        }

        private void selectedPropertyProperties_Click(object sender, EventArgs e)
        {
            if (shaderProps.SelectedGridItem != null)
            {
                object o = shaderProps.SelectedGridItem.Value;
                if (o != null)
                {
                    if (o is TextureResource)
                    {
                        TextureResource tex = o as TextureResource;
                        openTex.InitialDirectory = Directory.GetCurrentDirectory() + "\\Content\\Textures\\";
                        openTex.ShowDialog();
                        if (openTex.FileName.Length > 0)
                        {
                            string fullPath = openTex.FileName;
                            string finalTextureName = fullPath.Substring(openTex.InitialDirectory.Length);
                            finalTextureName = finalTextureName.Substring(0, finalTextureName.Length - 4);
                            tex.TextureName = finalTextureName;
                        }
                    }
                }
            }
        }
    }
}
